<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="IsAll" /> <span>全选</span></th>

          <th>姓名</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
          
        </tr>
      </thead>
      <tbody>
        <Gou
          v-for="(item, index) in goodList"
          :key="index"
          :name="item.name"
          :price="item.price"
          :index="index"
          :num="item.num"
          :checked="item.checked"
          :goodList="item"
          @jian="jian"
          @add="add"
          @shan="shan"  
          v-model="item.checked"     
        />
      </tbody>
      <tfoot>
        <zonghe :goodListArr="goodList" />
      </tfoot>
    </table>
  </div>
</template>

<script>
import Gou from './components/GouWu.vue'
import zonghe from './components/zonghe.vue'
export default {
  components: {
    Gou,
    zonghe
  },
  data() {
    return {
      goodList: [
        {
          name: '诸葛亮',
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: '蔡文姬',
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: '妲己',
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: '鲁班',
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    }
  },
  methods: {
    jian(i) {
      if(this.goodList[i].num<=0) return 
      this.goodList[i].num--
    },
    add(i) {
      this.goodList[i].num++
    },
    shan(i){
      this.goodList.splice(i,1)
    },
    
  },
     computed:{
    IsAll:{
      get(){
        return  this.goodList.every(item=>item.checked)
      },
      set(val){
        this.goodList.forEach((item)=>item.checked=val)
      }
    }
  },
  
}
</script>

<style></style>
